package com.maven.rabbitmq.simple;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ProduceDemo {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1. 建立连接： 首先 弄一个建立连接的 工厂，将 必要信息进行设置
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("8.135.54.49");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("study");
        connectionFactory.setPassword("study");
        connectionFactory.setVirtualHost("rabbitmq");

        // 2. 从 工厂中取出一个连接
        Connection connection = connectionFactory.newConnection();
        // 3. 建立信道
        Channel channel = connection.createChannel();

        /*
        * queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
                                 Map<String, Object> arguments)
        *           队列名称， 是否持久化，是否 独占，是否自动删除 ，参数
        * */
        // 4. 声明队列
        channel.queueDeclare("produced",true,false,true,null);

        // 5. 发送消息
        for(int i = 0; i < 10; i++) {
            String msg="hello ! my name is produce "+ i;
            channel.basicPublish("","produced",null,msg.getBytes());
        }

        System.out.println("发送成功");

        channel.close();
        connection.close();
    }
}
